﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

namespace Pixysoft.Framework.Logs.Core
{
    class Recorder : IRecorder
    {
        string path = null;

        string file = null;

        string postfix = null;


        public Recorder(string path, string file, string postfix)
        {
            this.path = path;
            this.file = file;
            this.postfix = postfix;
        }

        public bool Append(IDocument doc)
        {
            return Append(doc, false);
        }

        public bool Append(IDocument doc, bool directArchive)
        {
            if (doc == null)
                return false;

            StringBuilder builder = new StringBuilder();

            foreach (IRecord record in doc.Records)
            {
                AppendElement(builder, record);
            }

            string content = builder.ToString();

            SingletonLogger.Append(path, file, postfix, content, directArchive);

            return true;
        }

        public bool Insert(IDocument doc)
        {
            if (doc == null)
                return false;

            StringBuilder builder = new StringBuilder();

            foreach (IRecord record in doc.Records)
            {
                AppendElement(builder, record);
            }

            string content = builder.ToString();

            SingletonLogger.Insert(path, file, postfix, content);

            return true;
        }


        public string Append(IRecord record)
        {
            if (record == null)
                return null;

            StringBuilder builder = new StringBuilder();

            string id = AppendElement(builder, record);

            string content = builder.ToString();

            SingletonLogger.Append(path, file, postfix, content);

            return id;
        }

        public IDocument CreateDocument()
        {
            return new Document(path, file, postfix);
        }


        private string AppendElement(StringBuilder builder, IRecord record)
        {
            builder.Append("<");

            builder.Append(record.ElementName);

            foreach (IRecordProperty property in record.Properties)
            {
                builder.Append(" " + property.Key + "=\"" + property.Value + "\"");
            }

            builder.AppendLine(">");

            builder.AppendLine(record.Text);

            foreach (IRecord subrecord in record.Records)
            {
                AppendElement(builder, subrecord);
            }

            builder.AppendLine("</" + record.ElementName + ">");

            return record.Id;
        }

        private string GetId()
        {
            return Guid.NewGuid().ToString();
        }

        private string GetDate()
        {
            return Pixysoft.Tools.GlobalTimer.Instance.GetGlobalTime().ToString(StringHelper.default_log_dateformat);
        }

    }
}
